﻿<Window x:Class="EventCommandDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:local="clr-namespace:EventCommandDemo"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Background="{DynamicResource ContentBrush}"	
        xmlns:ts="clr-namespace:Eniux.Windows.EnterpriseLibrary.Commands;assembly=Eniux.Windows.EnterpriseLibrary"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>

        <ListView x:Name="list"				  
				  SelectedIndex="-1"
				  DockPanel.Dock="Top"
				  ItemsSource="{Binding Emails}"
				  IsSynchronizedWithCurrentItem="True"
				  Height="100">

            <ts:CommandSource.Triggers>
                <ts:CommandTriggerGroup>

                    <ts:EventCommandTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonUp"
											Command="{Binding Path=DownloadEmail}"
											CommandParameter="{Binding ElementName=list, Path=SelectedValue}" />

                    <ts:EventCommandTrigger RoutedEvent="UIElement.PreviewMouseRightButtonUp"
											Command="{Binding Path=MarkAsRead}"
											CommandParameter="{Binding ElementName=list, Path=SelectedValue}" />

                    <ts:EventCommandTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonDown"
											Command="{Binding Path=OpenEmail}"
											CommandParameter="{Binding ElementName=list, Path=SelectedValue}" />

                </ts:CommandTriggerGroup>
            </ts:CommandSource.Triggers>

            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Width="150" Header="From" DisplayMemberBinding="{Binding From.DisplayName}" />
                        <GridViewColumn Width="200" Header="Subject" DisplayMemberBinding="{Binding Subject}" />
                        <GridViewColumn Width="140" Header="Received" DisplayMemberBinding="{Binding Received}" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>

            <ListView.ItemContainerStyle>
                <Style>
                    <Setter Property="TextElement.FontWeight" Value="Bold" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsRead}" Value="True">
                            <Setter Property="TextElement.FontWeight" Value="Normal" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>

        </ListView>

        <ContentControl Content="{Binding Path=Emails/}">
            <ContentControl.ContentTemplate>
                <DataTemplate DataType="{x:Type local:EmailMessage}">

                    <Grid>

                        <StackPanel Margin="8">
                            <TextBlock Margin="0,0,0,8" FontSize="16" FontWeight="Bold" Text="{Binding Subject}" />
                            <TextBlock Margin="0,0,0,4" FontSize="14" Text="{Binding From.DisplayName}" />
                            <WrapPanel>
                                <TextBlock Text="Sent:" FontSize="10"  />
                                <TextBlock Margin="8,0,0,0" FontSize="10" Text="{Binding Received}" />
                            </WrapPanel>
                            <WrapPanel Margin="0,0,0,16">
                                <TextBlock Text="Attachments:" FontSize="10"  />
                                <TextBlock Margin="8,0,0,0" FontSize="10" Text="{Binding Attachment}" />
                            </WrapPanel>
                            <TextBlock Text="{Binding Content}" />

                        </StackPanel>

                        <ContentControl Content="{Binding Path=From}" Width="96" HorizontalAlignment="Right" Margin="0,6,6,0" Height="82" VerticalAlignment="Top">
                            <ContentControl.ContentTemplate>
                                <DataTemplate DataType="{x:Type local:Contact}">
                                    <Expander IsExpanded="{Binding Path=DummyProperty}" Header="Contact">

                                        <ts:CommandSource.Triggers>
                                            <ts:PropertyCommandTrigger Property="Expander.IsExpanded"
														   Value="True"
														   CommandParameter="{Binding}"
														   Command="{Binding Path=DownloadContact, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />
                                        </ts:CommandSource.Triggers>

                                        <Image x:Name="image" />

                                    </Expander>

                                    <DataTemplate.Triggers>
                                        <DataTrigger Binding="{Binding Path=IsDownloaded}" Value="True">
                                            <Setter TargetName="image" Property="Source" Value="{Binding Image}" />
                                        </DataTrigger>
                                    </DataTemplate.Triggers>

                                </DataTemplate>
                            </ContentControl.ContentTemplate>
                        </ContentControl>

                    </Grid>

                </DataTemplate>
            </ContentControl.ContentTemplate>
        </ContentControl>

    </DockPanel>
</Window>
